grstyle init
grstyle set plain
grstyle set legend 6, nobox
grstyle set color viridis, n(15) s(2 3 8 9 13 15)

set seed 119922

* set working directory: TO BE ADJUSTED
cd ""

use "dataset.dta", clear

svyset _n [pweight=inv_prob_allland]

** Social Preferences

* Cat Plots for Types

catplot soc_type treat [aweight=inv_prob_allland], perc(treat) asyvars stack legend(on rows(2) size(small) j(centered) symplacement(center) title("")) blabel(bar, pos(center) format(%2.1f) size(vsmall)) recast(bar)
graph export "Figures/soc_type_by_treat_pfo.png", replace
graph export "Figures/soc_type_by_treat_pfo.svg", replace

* Regressions

global controls i.base_subcounty strat_base_num_PFOs_vil strat_base_inc_pc_vil strat_base_dist_road_vil strat_base_l1_area_vil

quietly probit soc_type_spiteful i.treat i.enumerator $controls [pweight=inv_prob_allland], vce(cluster village_code)
scalar Nclust = e(N_clust)
scalar r2p = e(r2_p)
qui testparm i.treat i.enumerator $controls 
scalar p_value = r(p)
scalar chi = r(chi2)
eststo probit_soc_spiteful_pfo: quietly margins, dydx(*) atmeans post
estadd scalar p=p_value
estadd scalar N_clust=Nclust
estadd scalar r2_p=r2p
estadd scalar chi2=chi

quietly probit soc_type_generous i.treat i.enumerator $controls  [pweight=inv_prob_allland], vce(cluster village_code)
scalar Nclust = e(N_clust)
scalar r2p = e(r2_p)
qui testparm i.treat i.enumerator $controls 
scalar p_value = r(p)
scalar chi = r(chi2)
eststo probit_soc_generous_pfo: quietly margins, dydx(*) atmeans post
estadd scalar p=p_value
estadd scalar N_clust=Nclust
estadd scalar r2_p=r2p
estadd scalar chi2=chi

quietly probit soc_type_egalitarian i.treat i.enumerator $controls  [pweight=inv_prob_allland], vce(cluster village_code)
scalar Nclust = e(N_clust)
scalar r2p = e(r2_p) 
qui testparm i.treat i.enumerator $controls 
scalar p_value = r(p)
scalar chi = r(chi2)
eststo probit_soc_egali_pfo: quietly margins, dydx(*) atmeans post
estadd scalar p=p_value
estadd scalar N_clust=Nclust
estadd scalar r2_p=r2p
estadd scalar chi2=chi


** Forest use restrictions

tabout for_allow  for_no for_mon for_act_yn for_coll_oth for_conf treat using "Tables/for_restrict_items.htm" [aweight=inv_prob_allland] , replace style(htm) font(bold) c(col) svy f(3) mult(100) twidth(15) ptotal(none)
tabout for_firew_num for_poles_num for_deny for_paid for_coll for_conf_nfo  treat using "Tables/for_restrict_items_nfo.htm" [aweight=inv_prob_allland] , replace style(htm) font(bold) c(col) svy f(3) mult(100) twidth(15) ptotal(none)

foreach var in for_allow for_no for_mon for_act_yn for_coll_oth for_conf for_firew_num for_poles_num for_deny for_paid for_coll for_conf_nfo {
	tab `var' treat, chi2 col
}


* Regressions

eststo tobit_for_rest_for_own: quietly tobit for_restrictions i.treat i.enumerator $controls  [pweight=inv_prob_allland], vce(cluster village_code) ll(0) ul(1)
qui testparm i.treat i.enumerator $controls 
estadd scalar p_value = r(p)
estadd scalar Falt = r(F)
eststo tobit_for_rest_nfo: quietly tobit for_restrictions_nfo i.treat i.enumerator $controls   [pweight=inv_prob_allland], vce(cluster village_code) ll(0) ul(1)
qui testparm i.treat i.enumerator $controls 
estadd scalar p_value = r(p)
estadd scalar Falt = r(F)


** Social Norms

preserve
keep soc_consider? soc_village? hh_id treat inv_prob_allland
drop soc_consider1 soc_consider2 soc_village1 soc_village2
reshape long soc_, i(hh_id) j(var) string
encode var, gen(varn)
recode varn (1=1 "Personal Norm") (2=1) (3=1) (4=1) (5=2 "Social Norm") (6=2) (7=2) (8=2), gen(norm) 
recode varn (1=1 "Firewood money") (5=1) (2=2 "Firewood refusal") (6=2) (3=3 "Poles money") (7=3) (4=4 "Poles refusal") (8=4), gen(which) 

grstyle set color viridis, n(2) range(0.2, 0.85)
catplot soc_ treat which [aweight=inv_prob_allland], percent(treat norm which) by(norm) stack asyvars legend() blabel(bar, pos(center) format(%2.1f) size(vsmall))
gr_edit .plotregion1.grpaxis[2].draw_view.setstyle, style(no)
gr_edit .b1title.draw_view.setstyle, style(no)
gr_edit .note.draw_view.setstyle, style(no)
gr_edit .plotregion1.supaxis[2].draw_view.setstyle, style(no)
gr_edit .plotregion1.supaxis[1].style.editstyle majorstyle(tickangle(forty_five)) editcopy
gr_edit .plotregion1.supaxis[1].style.editstyle majorstyle(use_labels(no)) editcopy
gr_edit .plotregion1.supaxis[1].style.editstyle majorstyle(alternate(no)) editcopy
graph export "Figures/barplot_norms_by_treat.png", replace
graph export "Figures/barplot_norms_by_treat.svg", replace

restore


* Regressions

eststo tobit_pnorm_pfo: quietly tobit index_personalnorm i.treat i.enumerator $controls  [pweight=inv_prob_allland], vce(cluster village_code) ll(0) ul(1)
qui testparm i.treat i.enumerator $controls 
estadd scalar p_value = r(p)
estadd scalar Falt = r(F)

eststo tobit_snorm_pfo: quietly tobit index_socialnorm i.treat i.enumerator $controls  [pweight=inv_prob_allland], vce(cluster village_code) ll(0) ul(1)
qui testparm i.treat i.enumerator $controls 
estadd scalar p_value = r(p)
estadd scalar Falt = r(F)

* create table for main paper
quietly esttab probit_soc_spiteful_pfo probit_soc_generous_pfo probit_soc_egali_pfo tobit_pnorm_pfo	tobit_snorm_pfo tobit_for_rest_for_own	tobit_for_rest_nfo using "Tables/reg_paper1.rtf", l b(3) ci(3) mgroups("Social Preferences" "Norms" "Forest Restrictions", pattern(1 0 0 1 0 1 0)) mtitles("Spiteful" "Generous" "Egalitarian" "Personal" "Social" "Forest Owners" "Non-Forest Owners") eqlabels(none)  drop(*.base_subcounty strat_base_num_PFOs_vil strat_base_inc_pc_vil strat_base_dist_road_vil strat_base_l1_area_vil) nobase stats(N N_clust r2_p Falt p_value chi2 p, labels("N" "Clusters" "Pseudo R2" "F-Statistic" "p" "Chi2 Statistic" "p") f(%4.0f %4.0f %4.3f)) indicate("Enumerator FE = *.enumerator")  star(* 0.10 ** 0.05 *** 0.01) replace 
